Addison Wesley : Domain-Driven Design: Tackling Complexity in the Heart of Software by By Eric Evans
Author:By Eric Evans [Evans, By Eric]
Language: eng
Format: epub
ISBN: 0321125215
Publisher: Addison Wesley
Published: 0101-01-01T00:00:00+00:00
Example
Refactoring: A Paint-Mixing Application
A program for paint stores can show a customer the result of mixing standard paints. Here is the initial design, which has a single domain class.
Figure 10.2.
The only way to even guess what the paint(Paint) method does is to read the code.
public void paint(Paint paint) { v = v + paint.getV(); //After mixing, volume is summed // Omitted many lines of complicated color mixing logic // ending with the assignment of new r, b, and y values. }
OK, so it looks like this method combines two Paints together, the result having a larger volume and a mixed color.
To shift our perspective, let's write a test for this method. (This code is based on the JUnit test framework.)
public void testPaint() { // Create a pure yellow paint with volume=100 Paint yellow = new Paint(100.0, 0, 50, 0); // Create a pure blue paint with volume=100 Paint blue = new Paint(100.0, 0, 0, 50); // Mix the blue into the yellow yellow.paint(blue); // Result should be volume of 200.0 of green paint assertEquals(200.0, yellow.getV(), 0.01); assertEquals(25, yellow.getB()); assertEquals(25, yellow.getY()); assertEquals(0, yellow.getR()); }
The passing test is the starting point. It is unsatisfying at this point because the code in the test doesn't tell us what it is doing. Let's rewrite the test to reflect the way we would like to use the Paint objects if we were writing a client application. Initially, this test will fail. In fact, it won't even compile. We are writing it to explore the interface design of the Paint object from the client developer's point of view.
public void testPaint() { // Start with a pure yellow paint with volume=100 Paint ourPaint = new Paint(100.0, 0, 50, 0); // Take a pure blue paint with volume=100 Paint blue = new Paint(100.0, 0, 0, 50); // Mix the blue into the yellow ourPaint.mixIn(blue); // Result should be volume of 200.0 of green paint assertEquals(200.0, ourPaint.getVolume(), 0.01); assertEquals(25, ourPaint.getBlue()); assertEquals(25, ourPaint.getYellow()); assertEquals(0, ourPaint.getRed()); }
We should take our time to write a test that reflects the way we would like to talk to these objects. After that, we refactor the Paint class to make the test pass.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Brazilian Economy since the Great Financial Crisis of 20072008 by Philip Arestis Carolina Troncoso Baltar & Daniela Magalhães Prates(118542)
International Integration of the Brazilian Economy by Elias C. Grivoyannis(88359)
The Art of Coaching by Elena Aguilar(52890)
Flexible Working by Dale Gemma;(23242)
How to Stop Living Paycheck to Paycheck by Avery Breyer(19621)
The Acquirer's Multiple: How the Billionaire Contrarians of Deep Value Beat the Market by Tobias Carlisle(12213)
Thinking, Fast and Slow by Kahneman Daniel(12023)
The Radium Girls by Kate Moore(11899)
The Art of Thinking Clearly by Rolf Dobelli(10175)
Hit Refresh by Satya Nadella(9015)
The Compound Effect by Darren Hardy(8786)
Tools of Titans by Timothy Ferriss(8186)
Atomic Habits: Tiny Changes, Remarkable Results by James Clear(8157)
Turbulence by E. J. Noyes(7920)
A Court of Wings and Ruin by Sarah J. Maas(7620)
Change Your Questions, Change Your Life by Marilee Adams(7610)
Nudge - Improving Decisions about Health, Wealth, and Happiness by Thaler Sunstein(7543)
How to Be a Bawse: A Guide to Conquering Life by Lilly Singh(7360)
Win Bigly by Scott Adams(7078)